package itsm.isperp.module.service.bpm;

import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.bpm.BpmActionHistory;
import itsm.isperp.module.repository.bpm.BpmActionHistoryMapper;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

/**
 * 流程处理历史
 * 
 * @author lizx
 * 
 */
@Service
public class BpmActionHistoryService extends BaseService<BpmActionHistory> {

	@Autowired
	BpmActionHistoryMapper bpmActionHistoryMapper;

	@Override
	public BpmActionHistoryMapper getRepository() {
		return bpmActionHistoryMapper;
	}

	/**
	 * 保存处理过程
	 * 
	 * @param processId
	 * @param taskId
	 * @param actionFlowId
	 * @param actionName
	 * @param actionUserId
	 * @param comment
	 */
	public void save(String processId, String taskId, String actionFlowId,
			String actionName, String actionUserId, String comment) {

		BpmActionHistory entity = new BpmActionHistory();
		entity.setActionName(StringUtils.isEmpty(actionName) ? "提交"
				: actionName);
		entity.setActionTime(new Timestamp(new Date().getTime()));
		entity.setActionUserId(actionUserId);
		entity.setComment(comment);
		entity.setProcessId(processId);
		entity.setTaskId(taskId);
		entity.setActionFlowId(actionFlowId);
		this.saveOrUpdate(entity);
	}
	
	/**
	 * 保存处理过程
	 * 
	 * @param processId
	 * @param taskId
	 * @param actionFlowId
	 * @param actionName
	 * @param actionUserId
	 * @param comment
	 */
	public void save(String processId, String relationId, String taskId,
			String actionFlowId, String actionName, String actionUserId,
			String comment) {

		BpmActionHistory entity = new BpmActionHistory();
		entity.setActionName(StringUtils.isEmpty(actionName) ? "提交"
				: actionName);
		entity.setActionTime(new Timestamp(new Date().getTime()));
		entity.setActionUserId(actionUserId);
		entity.setComment(comment);
		entity.setProcessId(processId);
		entity.setTaskId(taskId);
		entity.setActionFlowId(actionFlowId);
		
		this.saveOrUpdate(entity);
	}


	/**
	 * 取得开始节点
	 * 
	 * @param processId
	 * @return
	 */
	public BpmActionHistory findStartByProcessId(String processId) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("process_id", JqGridFilterRuleOp.eq, processId);

		req.addFilterRule("task_id", JqGridFilterRuleOp.isnull, null);

		req.addFilterRule("action_flow_id", JqGridFilterRuleOp.isnull, null);
		List<BpmActionHistory> list = bpmActionHistoryMapper
				.findAllByRequest(req);
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	/**
	 * 根据processId获取历史处理
	 * 
	 * @param processId
	 * @return
	 */
	public List<BpmActionHistory> findByProcessId(String processId) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("process_id", JqGridFilterRuleOp.eq, processId);
		req.setSort("action_time", Direction.ASC);
		return bpmActionHistoryMapper.findAllByRequest(req);
	}

	/**
	 * 根据processId获取历史处理
	 * 
	 * @param processId
	 * @return
	 */
	public List<Map<String, Object>> findDetailByProcessId(String processId) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("process_id", JqGridFilterRuleOp.eq, processId);
		req.setSort("action_time", Direction.ASC);
		return bpmActionHistoryMapper.findAllDetailByRequest(req);
	}

	/**
	 * 取得开始节点
	 * 
	 * @param processId
	 * @return
	 */
	public BpmActionHistory findStartByBusinessKey(String businessKey) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.addFilterRule("relation_id", JqGridFilterRuleOp.eq, businessKey);

		req.addFilterRule("task_id", JqGridFilterRuleOp.isnull, null);

		req.addFilterRule("action_flow_id", JqGridFilterRuleOp.isnull, null);
		List<BpmActionHistory> list = bpmActionHistoryMapper
				.findAllByRequest(req);
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}
}
